package com.zimmur.app.mocolink.manage.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.model.CommonQuestion;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.ICommonQuestionService;

@Controller
@RequestMapping("/question")
public class CommonQuestionController extends BaseController {
	@Autowired
	private ICommonQuestionService commonQuestionService;

	@RequestMapping("/index")
	public String index(ModelMap model) {
		List<CommonQuestion> list = commonQuestionService.selectAll();
		model.addAttribute("questionList", list);
		return "/center/commonQuestion/list";
	}

	@RequestMapping("/edit")
	public String edit(ModelMap model) {
		Integer questionId = getParam("questionId", 0);
		CommonQuestion question = commonQuestionService.selectById(questionId);
		model.addAttribute("question", question);
		return "/center/commonQuestion/edit";
	}

	@RequestMapping("/add")
	public String add(ModelMap model) {
		CommonQuestion question = new CommonQuestion();
		model.addAttribute("question", question);
		return "/center/commonQuestion/edit";
	}

	@ResponseBody
	@RequestMapping("/save")
	public ReturnJsonEntity save(CommonQuestion question) {
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		int flag = 0;
		if (question.getId() != null && question.getId() > 0) {
			flag = commonQuestionService.update(question);
		} else {
			question.setId(null);
			flag = commonQuestionService.add(question);
		}
		if (flag > 0) {
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/delete")
	public ReturnJsonEntity delete() {
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer questionId = getParam("questionId", 0);
		int flag = commonQuestionService.delete(questionId);
		if (flag > 0) {
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/updateStatus")
	public ReturnJsonEntity updateStatus() {
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer questionId = getParam("questionId", 0);
		CommonQuestion question = commonQuestionService.selectById(questionId);
		if(question!=null){
			if(question.getIsShow()==Constant.byte_0){
				question.setIsShow(Constant.byte_1);
			}else{
				question.setIsShow(Constant.byte_0);
			}
			
			int flag = commonQuestionService.update(question);
			if (flag > 0) {
				codeEnum = StatusCodeEnum.CODE000000;
			}
		}else{
			codeEnum = StatusCodeEnum.CODE401401;
		}
		entity.init(codeEnum);
		return entity;
	}

}
